#include <iostream>
using namespace std;
int divide(int dividend, int divisor)
{
    //if (dividend < INT_MIN || divisor < INT_MIN || dividend > INT_MAX || divisor > INT_MAX)
    //    return INT_MAX;
    //暂不考虑溢出
    int tok = (dividend > 0 && divisor > 0) || (dividend < 0 && divisor < 0) ? 1 : -1;
    dividend = abs(dividend);
    divisor = abs(divisor);
    if (divisor == 1)
    {
        cout<<"返回";
        return tok==1?((unsigned)dividend>INT_MAX?INT_MAX:dividend):((unsigned)dividend>(INT_MAX+1)?INT_MAX:dividend);
    }
    //函数计算向下取整的结果
    int temp = divisor;
    int i = 1;
    while (true)
    {
        if (temp < dividend)
        {
            if (((unsigned)temp + (unsigned)temp) > (tok == 1 ? INT_MAX : INT_MAX+1))
            {
                //发生溢出，直接返回i
                return i * tok;
            }
            temp += temp;
            i += i;
        }
        else if (temp == dividend)
        {
            return i * tok;
        }
        else
        {
            if (i == 1)
                return 0; //返回被除数小于除数的结果
            if (temp - divisor <= dividend)
            {

                return (i - 1) * tok;
            }
            else
            {
                temp -= divisor;
                i -= 1;
            }
        }
        cout << temp << " " << divisor << " " << temp - divisor << endl;
    }
}
int main()  
{
    cout << divide(-2147483648, -1) << endl; //溢出，需返回intmax
    cout << divide(-2147483648, 1) << endl; 
    return 0;
}